<?php

class ControllerUserUserDefine extends Controller
{

    private $error = array();


    public function index()
    {

        $this->load->language('lottery/lottery');

        $this->document->setTitle('积分简介');


        $this->load->model('user/user_define');

        $this->getUserDefines();
    }


    public function edit(){

        $this->document->setTitle('编辑简介');

        $this->load->model('user/user_define');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_user_user_define->editUserDefine($this->request->get['user_define_id'],$this->request->post);
            $this->session->data['success'] = "编辑成功";

            $this->response->redirect($this->url->link('user/user_define', 'token=' . $this->session->data['token'] , 'SSL'));
        }

        $this->getUserDefineForm();
    }


    protected function validateForm() {


        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }


        if (utf8_strlen($this->request->post['name']) < 1 ) {
            $this->error['name'] = "请输入名字";
        }
        if (utf8_strlen($this->request->post['description']) < 1 ) {
            $this->error['description'] = "请输入描述";
        }

        return !$this->error;
    }




    protected function getUserDefines()
    {


        $data['userdefines'] = array();

        $results = $this->model_user_user_define->getUserDefines();

        foreach ($results as $result) {
            $data['userdefines'][] = array(
                'user_define_id' => $result['user_define_id'],
                'name' => $result['name'],
                'date_modify' => $result['date_modify'],
                'edit' => $this->url->link('user/user_define/edit', 'token=' . $this->session->data['token'] . '&user_define_id=' . $result['user_define_id'] , 'SSL')
            );
        }

        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['text_missing'] = $this->language->get('text_missing');


        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');
        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array) $this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }


        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $this->response->setOutput($this->load->view('user/user_define_list.tpl', $data));
    }



    public function getUserDefineForm()
    {
        $this->load->model('user/user_define');

        $data = array();


        $data['text_form'] = ! isset($this->request->get['user_define_id']) ? '添加简介' : '编辑简介';


        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        }

        if (isset($this->error['name'])) {
            $data['error_name'] = $this->error['name'];
        }
        if (isset($this->error['description'])) {
            $data['error_description'] = $this->error['description'];
        }



        $data['cancel'] = $this->url->link('user/user_define', 'token=' . $this->session->data['token'] , 'SSL');

        if (isset($this->request->get['user_define_id']) ) {
            $user_define_info = $this->model_user_user_define->getUserDefine($this->request->get['user_define_id']);
        }


        if (isset($this->request->post['name'])) {
            $data['name'] = $this->request->post['name'];
        } elseif (!empty($user_define_info)) {
            $data['name'] = $user_define_info['name'];
        } else {
            $data['name'] = '';
        }

        if (isset($this->request->post['description'])) {
            $data['description'] = $this->request->post['description'];
        } elseif (!empty($user_define_info)) {
            $data['description'] = $user_define_info['description'];
        } else {
            $data['description'] = '';
        }





        if (!isset($this->request->get['user_define_id'])) {
            $data['action'] = $this->url->link('user/user_define', 'token=' . $this->session->data['token'] , 'SSL');
        } else {
            $data['action'] = $this->url->link('user/user_define/edit', 'token=' . $this->session->data['token'] . '&user_define_id=' . $this->request->get['user_define_id'] , 'SSL');
        }

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('user/user_define_form.tpl', $data));
    }
}